const express = require('express');
const server = express();
const cors = require('cors');
const bodyParser = require('body-parser');


//连接池
const mysql = require('mysql');
const {
  query
} = require('express');
const {
  Console
} = require('console');
const pool = mysql.createPool({
  host: '127.0.0.1',
  port: 3306,
  user: 'root',
  password: '',
  database: 'xh',
  connectionLimit: 10
})

server.use('/public', express.static('./public'));

// 配置跨域cors
// server.use(function(req,res,next){
// 	res.header('Access-Control-Allow-Origin', 'http://localhost:8080',"http://192.168.2.102:8080");
// 	next();
// })
server.use(cors({
  origin: ['http://localhost:8080', "http://192.168.2.102:8080"],
  credentials: true
}))


// node第三方模块body-parser的urlencoded方法的extended参数说明
server.use(bodyParser.urlencoded({
  extended: false
}));
//--------------------------------------------------------
// 1、轮播图
server.get('/banner', (req, res) => {
  var sql = "select * from banner";
  pool.query(sql, (err, result) => {
    if (err) throw err;
    res.json(result)
  })
});

//2、首页列表
server.get('/list', (req, res) => {
  var sql = "select * from list";
  pool.query(sql, (err, result) => {
    if (err) throw err;
    res.send(result);
  })
});

//3、 获取详情
server.get('/details', (req, res) => {
  let d_id = req.query.d_id;
  var sql = "select * from details where d_id=?";
  pool.query(sql, [d_id], (err, result) => {
    if (err) throw err;
    res.send({
      results: result
    })
  })
});

//4、 获取商品分类名称
server.get('/fenlei', (req, res) => {
  // let fid=req.query.fid;
  var sql = "select * from fenlei";
  pool.query(sql, (err, result) => {
    if (err) throw err;
    res.json(result)
  })
});

//5、 获取商品分类的商品
server.get("/classify", (req, res) => {
  let fid = req.query.fid;
  let sql = "select * from list inner join fenlei on fenlei_fid=fid where fid=?";
  pool.query(sql, [fid], (err, result) => {
    if (err) throw err;
    res.json(result)
  })
})

//6、用户注册的接口
server.post('/register', (req, res) => {
  // 获取用户名和密码信息
  let uname = req.body.uname;
  //以username为条件进行查找操作，以保证用户名的唯一性
  let sql = 'select * from register where uname=?';
  pool.query(sql, [uname], (error, results) => {
    if (results.length == 0) {
      // 将用户的相关信息插入到数据表
      let obj = req.body;
      let sql = 'insert into register (uname,upwd,reset_upwd)values(?,?,?) ';
      pool.query(sql, [obj.upwd, obj.uname, obj.reset_upwd], (error, results) => {
        res.send({
          code: 200,
          msg: "注册成功"
        })
      })
    }else{
      res.send({
        code: 201,
        msg: '用户已存在'
      });
    }
  });
});



//7、用户登录接口
server.post('/account', (req, res) => {
  //获取用户输入的用户名和密码信息
  let obj = req.body;
  console.log(obj);
  //现在要以输入的用户名和密码为条件进行查找
  let sql = 'SELECT phone,upwd FROM account WHERE phone=? AND upwd=?';
  pool.query(sql, [obj.phone, obj.upwd], (error, results) => {
    if (error) throw error;
    //如果找到，则代表用户登录成功
    if (results.length == 1) {
      res.json({
        message: '登陆成功',
        code: 200,
        info: results[0]
      })
    } else { //否则代表用户登录失败
      res.json({
        message: '登录失败',
        code: 0
      })
    }
  })
});

// 8、购物车
server.post('/tocar',(req,res)=>{
  let s_id=req.body.s_id;
  let s_img=req.body.s_img;
  let s_title=req.body.s_title;
  let s_price=req.body.s_price;
  let sh_price=req.body.sh_price;
  let sql="insert into shoppingcart(s_img,s_title,s_price,sh_price,s_id) values (?,?,?,?,?)";
  pool.query(sql,[s_img,s_title,s_price,sh_price,s_id],(err,result)=>{
    if(err) throw err;
    res.send(result)
  })
})
server.get('/car',(req,res)=>{
  var sql="select * from shoppingcart";
  pool.query(sql,(err,result)=>{
    if (err) throw err;
    res.send(result);
  })
})


server.listen(3000, () => {
  console.log('server is running...');
});